package com.dyz.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ReconstructQueue406 {
    public int[][] reconstructQueue(int[][] people) {
        int n = people.length;
        List<int[]> res = new ArrayList<>();

        Arrays.sort(people, (o1, o2)->(o1[0]==o2[0]?o1[1]-o2[1]:o2[0]-o1[0]));
        for (int[] p :people){
            res.add(p[1], p);
        }

        int[][] ans = new int[n][2];
        for (int i=0; i<res.size();i++){
            ans[i][0] = res.get(i)[0];
            ans[i][1] = res.get(i)[1];
        }
        return ans;
    }
}
